﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Admin.Common
{
    /// <summary>
    /// 加密帮助类
    /// </summary>
    public class EncryptionHelper
    {
        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="str">加密串</param>
        /// <param name="salt">盐</param>
        /// <returns></returns>
        public static string Md5(String str, string salt)
        {
            return BitConverter.ToString(MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(str + salt))).Replace("-", "");
        }

        /// <summary>
        /// 生成随机数盐
        /// </summary>
        /// <returns></returns>
        public static string Salt()
        {
            return BitConverter.ToString(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(GenerateRandomCode()))).Replace("-", "");
        }


        /// <summary>
        ///生成制定位数的随机码（数字）
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GenerateRandomCode(int length = 6)
        {
            var result = new StringBuilder();
            for (var i = 0; i < length; i++)
            {
                var r = new Random(Guid.NewGuid().GetHashCode());

                result.Append(r.Next(0, 10));
            }
            return result.ToString();
        }
    }
}
